//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.controller
{
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	import flash.utils.ByteArray;
	
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	
	import reader.ApplicationFacade;
	import reader.core.ReaderResource;
	import reader.model.vo.UploadPhotoVO;

	public class GetImageDataCommand extends SimpleCommand implements ICommand
	{
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		override public function execute(notification:INotification):void
		{
			var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png, *.bmp, *.tiff)", "*.jpg; *.jpeg; *.gif; *.png; *.bmp; *.tiff", "JPEG; GIFf; GIF; jp2_; TIFF; PNG; BMP");
			var uploader:File = new File();
			uploader.addEventListener(Event.SELECT, onPhoroSelect_handler)
			try
			{
			   uploader.browseForOpen(ReaderResource.instance.resource.insertPhotouploadBrowseForOpen, [imageTypes]);
			}
			catch (error:Error)
			{
			    
			}  
		}
		private function onPhoroSelect_handler( e:Event ):void
		{
			var image:ByteArray = new ByteArray();
			var stream:FileStream = new FileStream();
			var file:File = e.target as File;
			file.removeEventListener(Event.SELECT, onPhoroSelect_handler);
			stream.open( file, FileMode.READ );
			stream.readBytes( image, 0, stream.bytesAvailable );
			stream.close();
			
			var uploadPhoto:UploadPhotoVO = new UploadPhotoVO();
			uploadPhoto.image = image;
			uploadPhoto.fileName = file.name;
			uploadPhoto.nativePath = file.nativePath;
			uploadPhoto.extension = file.extension;
			uploadPhoto.size = file.size;
			uploadPhoto.createData = file.creationDate;
			uploadPhoto.modificationDate = file.modificationDate;
			
			if(image) sendNotification(ApplicationFacade.PHOTO_FILE_DATA, uploadPhoto);
		}
		
	}
}